package com.ewhale.feitengguest.ui.user.mine

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.ewhale.feitengguest.R
import com.ewhale.feitengguest.dto.ArticleDetailDto
import com.ewhale.feitengguest.dto.HelpCenterDto
import com.ewhale.feitengguest.presenter.user.mine.HelpCenterPresenter
import com.ewhale.feitengguest.ui.WebViewActivity
import com.ewhale.feitengguest.ui.user.mine.adapter.HelpCenterAdapter
import com.ewhale.feitengguest.view.user.mine.HelpCenterView
import com.simga.library.activity.MBaseActivity
import com.simga.library.utils.Dp2PxUtil
import com.simga.library.widget.RecycleViewDivider
import kotlinx.android.synthetic.main.activity_helpcenter.*

class HelpCenterActivity : MBaseActivity<HelpCenterPresenter>(), HelpCenterView {

    private val datas by lazy(LazyThreadSafetyMode.NONE) { ArrayList<HelpCenterDto>() }
    private var mAdapter: HelpCenterAdapter? = null
    override fun onGetBundle(bundle: Bundle) {}
    override fun getViewId(): Int {
        return R.layout.activity_helpcenter
    }

    override fun init(savedInstanceState: Bundle?) {
        setTitle("帮助中心")
        ref_layout!!.apply {
            setEnableLoadmore(false)
            setEnableRefresh(false)
        }
        mAdapter = HelpCenterAdapter(datas)
        list!!.apply {
            layoutManager = LinearLayoutManager(mContext)
            setHasFixedSize(true)
            addItemDecoration(RecycleViewDivider(mContext,
                    Dp2PxUtil.dip2px(mContext, 1f)))
            adapter = mAdapter
        }
        presenter!!.loadlist()
    }

    override fun isUseBaseTitleBar(): Boolean {
        return true
    }

    override fun initListener() {
        mAdapter!!.setOnItemClickListener { _, position -> presenter!!.loadArticleDetails(datas[position]) }
    }

    override fun showProLoading() {
        showLoading()
    }

    override fun dimissProLoading() {
        dismissLoading()
    }

    override fun showErrorMessage(postCode: Int, code: String, message: String) {
        showErrorMsg(code, message)
    }

    override fun showList(result: List<HelpCenterDto>) {
        datas.addAll(result)
        mAdapter!!.notifyDataSetChanged()
    }

    override fun showDetail(result: ArticleDetailDto, data: String) {
        WebViewActivity.open(mContext, data, result.content)
    }

    companion object {
        @JvmStatic
        fun open(context: MBaseActivity<*>) {
            context.startActivity(null, HelpCenterActivity::class.java)
        }
    }
}